<template>
  <div id="app">
    <div id="container">
      <div class="header">
        <img
          src="./assets/appicon.png"
          alt=""
          style="width:20px;height:20px;margin:5px"
        />
        <span class="maintitle">Electron-Vue-Ant v{{ version }}</span>
        <ul class="controlbtn">
          <li @click="minwin">
            <i class="iconfont icon-zuixiaohua" id="minbtn"></i>
          </li>
          <li @click="maxwin">
            <i class="iconfont icon-zuidahua" id="maxbtn"></i>
          </li>
          <li @click="closewin">
            <i class="iconfont icon-closebtn" id="closebtn"></i>
          </li>
        </ul>
      </div>
      <Ohyeah>
        <div class="contenter">
          <div class="logo"><img alt="Vue logo" src="./assets/logo.png" /></div>
          <Mainpage msg="Welcome to Your Electron+Vue+Ant-design-vue App." />
        </div>
      </Ohyeah>
      <div class="footer">
        <div class="copy">&copy; Yourweb.com</div>
        <div style="display:flex;">
          <a-progress
            :percent="percentage"
            size="small"
            style="width:100px;margin-top:4px;margin-right:10px"
            status="active"
            v-if="downloadinfo"
          /><span class="info">{{ updateTips }}</span>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
//const { ipcRenderer } = require("electron");

const { ipcRenderer } = require("electron");
import Mainpage from "./page/mainpage.vue";
import config from "../package.json";

export default {
  name: "App",
  data() {
    return {
      themeMode: "normal",
      updateTips: "",
      downloadinfo: false,
      percentage: 0,
      version: "",
    };
  },
  mounted() {
    let _this = this;
    _this.version = config.version;
    //接收主进程版本更新消息
    ipcRenderer.on("message", (event, arg) => {
      // for (var i = 0; i < arg.length; i++) {
      console.log(arg);
      if ("update-available" == arg.cmd) {
        //显示升级对话框
        _this.$message.info("发现新的版本");
        _this.updateTips = "发现新版本";
        _this.dialogVisible = true;
      } else if ("download-progress" == arg.cmd) {
        _this.updateTips = "下载中...";
        _this.downloadinfo = true;
        //更新升级进度
        /*
	      message{bytesPerSecond: 47673
	      delta: 48960
	      percent: 0.11438799862426002
	      total: 42801693
	      transferred: 48960
	      }
	     */
        console.log(arg.message.percent);
        let percent = Math.round(parseFloat(arg.message.percent));
        _this.percentage = percent;
      } else if ("error" == arg.cmd) {
        _this.dialogVisible = false;
        _this.$message.error("更新失败");
      } else if ("update-downloaded" == arg.cmd) {
        //_this.$message.info("下载完成");
        _this.downloadinfo = false;
        _this.updateTips = "";
        _this.$confirm({
          content: <div>下载完成，是否立即重启？</div>,
          onOk() {
            console.log("OK");
            ipcRenderer.send("resetApp");
          },
          onCancel() {
            console.log("Cancel");
          },
        });
      }
      // }
    });
    ipcRenderer.send("checkForUpdate");
  },
  components: {
    Mainpage,
  },
  methods: {
    minwin() {
      console.log("minwin");
      ipcRenderer.send("minMainWin");
    },
    maxwin() {
      console.log("maxwin");
      ipcRenderer.send("maxMainWin");
    },
    closewin() {
      console.log("closewin");
      ipcRenderer.send("closeMainWin");
    },
  },
};
</script>

<style lang="less">
body {
  overflow: hidden;
}
ul,
li {
  margin: 0;
  padding: 0;
}
ul {
  list-style: none;
}
#container img {
  width: 600px;
}
.logo {
  text-align: center;
}
#container {
  display: flex;
  flex-direction: column;
  height: 100vh;
}

.header {
  -webkit-app-region: drag;
  background-color: #3a3a3a;
  height: 30px;
  display: flex;
  justify-content: space-between;
}
.header span {
  color: #ccc;
  line-height: 30px;
  padding: 0 10px;
}
.contenter {
  flex: 1;
  overflow-x: hidden;
  overflow-y: auto;
}
.footer {
  line-height: 30px;
  display: flex;
  padding: 0 13px;
  justify-content: space-between;
  font-size: 12px;
}
.controlbtn {
  color: #fff;
  float: right;
  line-height: 30px;
}
.controlbtn li {
  cursor: pointer;
  float: left;
  height: 30px;
  padding: 0px 14px;
  -webkit-app-region: no-drag;
}
.controlbtn li:hover {
  background: #444;
}
.controlbtn li:nth-child(3):hover {
  background: #c70404;
}

.mycard .ant-card-body {
  padding: 10px;
  padding-top: 0 !important;
}
.controlbtn .iconfont {
  font-size: 14px;
}

* {
  -webkit-touch-callout: none; /*系统默认菜单被禁用*/
  -webkit-user-select: none; /*webkit浏览器*/
  -khtml-user-select: none; /*早期浏览器*/
  -moz-user-select: none; /*火狐*/
  -ms-user-select: none; /*IE10*/
  user-select: none;
}
input {
  -webkit-user-select: auto; /*webkit浏览器*/
}
textarea {
  -webkit-user-select: auto; /*webkit浏览器*/
}
</style>
